from pms.common.views import PostFormula
from project.models import Project
from user.models import User
from project.utils.member import project_add_member
from project.utils.profile import member_view
from project.utils.permission import assert_project_permissions


class API(PostFormula):

    def handle(self, req):
        pid = req['pid']
        member = req['member']
        member_id = req['member']['id']
        rid = req['member']['role']['id']
        assert_project_permissions(pid, self.user.id, [3])
        project, user = project_add_member(
            project=Project.objects.get(id=pid),
            user=User.objects.get(id=member_id),
            rid=rid,
            view=member
        )
        return self.success_respond(member_view(project, user))
